import * as echarts from '../../../ec-canvas/echarts';
const baseURL = require('../../../utils/baseURL')



// chart为图表实例，记得要声明为全局的
var chart = null

function initChart(canvas, width, height, dpr) {
    chart = echarts.init(canvas, null, {
        width: width,
        height: height,
        devicePixelRatio: dpr
    });
    chart.showLoading() //显示Loading
    canvas.setChart(chart);

    var option = {
        title: {
            text: '获取数据中',
            left: 'center'
        },
    };

    chart.setOption(option);
    return chart;

}
var genChart
var genResult
Page({
    data: {
        ec: {
            onInit: initChart
        },

    },
    onLoad() {
        // this.getData()
    },

    getData() {
        //这里是模拟的数据请求，项目中请使用wx.request替换掉setTimeouot
        console.log('发送请求~~~~')
        const token = wx.getStorageSync('token')
        wx.request({
            url: baseURL.dev + '/wxmini/getdatachart',
            data: {
                chartType: "柱状图"
            },
            header: {
                'Authorization': token
            },
            method: 'POST',
            success(res) {
                console.log('post请求完毕,data：', res.data)
                genChart = res.data.data.genChart
                genResult = res.data.data.genResult
                console.log('genChart', genChart)
                console.log('genResult', genResult)

            }
        })
        setTimeout(() => {
            chart.hideLoading() //隐藏Loading
            chart.setOption({
                "title": {
                    "text": "阿尔兹海默症患者康复训练游戏得分变化折线图"
                },
                "xAxis": {
                    "type": "category",
                    "data": ["第1次", "第2次", "第3次", "第4次", "第5次", "第6次", "第7次", "第8次", "第9次", "第10次", "第11次", "第12次", "第13次", "第14次", "第15次", "第16次", "第17次", "第18次", "第19次", "第20次", "第21次", "第22次", "第23次", "第24次", "第25次", "第26次", "第27次", "第28次", "第29次"]
                },
                "yAxis": {
                    "type": "value",
                    "min": 0,
                    "max": 100
                },
                "series": [{
                    "data": [67, 46, 35, 13, 54, 34, 75, 56, 76, 75, 75, 76, 77, 78, 79, 81, 72, 71, 75, 72, 71, 74, 74, 76, 83, 81, 83, 62, 75],
                    "type": "line",
                    "smooth": true
                }]
            })
        }, 1000);

    },
    onReady() {
        console.log('bar页面')
        //获取数据
        this.getData()
    },
});